<?php
final class Functions {
  	public function __construct() {
		
	}
	
	public function trunchtml($text = "", $chars=150, $ending='', $exact='no', $inline='', $threshold='') {

    $raw = strlen(preg_replace('/<.*?>
/', '', $text));         
          if ( $raw <= $chars) {
              $return_data = $text;
              return $return_data;
          }
                    if ( $threshold > 0 && $raw < $threshold )
                    {
                        $return_data = $text;
                        return $return_data ;
                    }
          preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
          $total_length = 0;
          $open_tags = array();
          $truncate = '';
          foreach ($lines as $line_matchings) {
              if (!empty($line_matchings[1])) {
                  if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) 
                  {
                  } 
                  elseif (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) 
                  {
                      $pos = array_search($tag_matchings[1], $open_tags);
                      if ($pos !== false) 
                      {
                          unset($open_tags[$pos]);
                      }
                  } 
                  elseif (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) 
                  {
                      array_unshift($open_tags, strtolower($tag_matchings[1]));
                  }
                  $truncate .= $line_matchings[1];
              }
              $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));

              if ($total_length + $content_length > $chars) 
              {
                  $left = $chars - $total_length;
                  $entities_length = 0;
                  if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE|PREG_PATTERN_ORDER)) 
                  {
                      foreach ($entities[0] as $entity) 
                      {
                          if ($entity[1] + 1 - $entities_length <= $left) 
                          {
                              $left--;
                              $entities_length += strlen($entity[0]);
                          } 
                          else 
                          {
                              break;
                          }
                      }
                  }
                  $truncate .= substr($line_matchings[2], 0, $left + $entities_length);
                  break;
              } 
              else 
              {
                  $truncate .= $line_matchings[2];
                  $total_length += $content_length;
              }
              if ($total_length >= $chars) {
                  break;
              }
          }
          if ($exact != "yes") {
                            $last_gt = strrpos($truncate, '>');
                            $spacepos = strrpos($truncate, ' ');
                            if ( ($last_gt !== FALSE && $spacepos !== FALSE) && $last_gt > $spacepos ) 
                            {
                                $spacepos = strrpos($truncate, '<');
                                if ($spacepos !== FALSE)
                                {
                                    $truncate = substr($truncate, 0, $spacepos);
                                    array_shift($open_tags);
                                }
                            }
                            elseif ( $spacepos !== FALSE ) 
              {
                  $truncate = substr($truncate, 0, $spacepos);
              }
          }
          $truncate = rtrim($truncate);
          if (!empty($inline)) {
                if (substr($inline,0,1)=="_") {
                    $inline = " ".ltrim($inline,"_");
                }
              $truncate .= $inline;
          }
          foreach ($open_tags as $tag) {
              $truncate .= '</' . $tag . '>';
          }
          if ( !empty($ending) ) {
              $truncate .= $ending;
          }
          return $truncate;

      }
	  
	  //Xu Ly Tieng viet co dau thanh khong dau
	  function RemoveSign($str)
		{
		$coDau=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă","ằ","ắ"
		,"ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề","ế","ệ","ể","ễ","ì","í","ị","ỉ","ĩ",
		"ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
		,"ờ","ớ","ợ","ở","ỡ",
		"ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
		"ỳ","ý","ỵ","ỷ","ỹ",
		"đ",
		"À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
		,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
		"È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
		"Ì","Í","Ị","Ỉ","Ĩ",
		"Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
		,"Ờ","Ớ","Ợ","Ở","Ỡ",
		"Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
		"Ỳ","Ý","Ỵ","Ỷ","Ỹ",
		"Đ","ê","ù","à");
		$khongDau=array("a","a","a","a","a","a","a","a","a","a","a"
		,"a","a","a","a","a","a",
		"e","e","e","e","e","e","e","e","e","e","e",
		"i","i","i","i","i",
		"o","o","o","o","o","o","o","o","o","o","o","o"
		,"o","o","o","o","o",
		"u","u","u","u","u","u","u","u","u","u","u",
		"y","y","y","y","y",
		"d",
		"A","A","A","A","A","A","A","A","A","A","A","A"
		,"A","A","A","A","A",
		"E","E","E","E","E","E","E","E","E","E","E",
		"I","I","I","I","I",
		"O","O","O","O","O","O","O","O","O","O","O","O"
		,"O","O","O","O","O",
		"U","U","U","U","U","U","U","U","U","U","U",
		"Y","Y","Y","Y","Y",
		"D","e","u","a");
		return str_replace($coDau,$khongDau,$str);
		}
	  
}
?>